﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using spbtzb;

public partial class Back_ChooseLinkUrl : System.Web.UI.Page
{
    #region Field 
    protected String StrConn = ConString.ConnString;
    private Boolean session_Logined
    {
        set
        {
            Session["Logined"] = value;
        }
        get
        {
            return Session["Logined"] == null ? false : Convert.ToBoolean(Session["Logined"]);
        }
    }

    private DataTable viewState_DtSrc
    {
        get
        {
            return ViewState["DtSrc"] == null ? null : (DataTable)ViewState["DtSrc"];
        }
        set
        {
            ViewState["DtSrc"] = value;
        }
    }
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (session_Logined == false)
            {
                Response.Redirect("Login.aspx?ErrorMessage=" + "您未登录，请在此登录");
            }

            string Type = Request.Params["Type"];
            if (Type == "PPT" | Type == "TPXW")
            {
                Panel_WenZhang.Visible = true;
                Panel_ZhuanTi.Visible = false;
                Ddl_Types_BindData();
                this.ddl_Types.SelectedIndex = this.ddl_Types.Items.Count - 1;
                ddl_Types_SelectedIndexChanged(new Object(), new EventArgs());

            }
            else
            {
                Panel_WenZhang.Visible = false;
                Panel_ZhuanTi.Visible = true;
                BindSpecialTops();
            }

        }
    }

    private void BindSpecialTops()
    {
        ddlZhuanTi.DataSource = AccessHelper.ExecuteDataTable(ConString.ConnString,CommandType.Text,"SELECT * FROM ContentType WHERE ParentType='ST'");
        ddlZhuanTi.DataTextField = "TypeName";
        ddlZhuanTi.DataValueField = "TypeCode";
        ddlZhuanTi.DataBind();
    }

    private void Ddl_Types_BindData()
    {
        // generate datatable                       
        string sqlCmd = "SELECT * FROM ContentType WHERE ParentType = 'NO' OR ParentType = 'NOBB' OR ParentType = 'STBB'";
        System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
        if (dtTypes.Rows.Count == 0)
        {
            // 没有相应类型的信息
            ScriptManager.RegisterStartupScript(UpdatePanel1, Page.GetType(), "deletesuccess", "alert('没有相应的类型数据！');", true);
            return;
        }
        this.ddl_Types.DataSource = dtTypes;
        this.ddl_Types.DataBind();
        this.ddl_Types.Items.Add(new ListItem("全部", "all"));
    }

    protected void ddl_Types_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddl_Types.SelectedValue == "all")
        {
            this.ddl_ChildTypes.Visible = false;
            string sqlCmd = "SELECT Contents.*, ContentType.TypeName FROM Contents INNER JOIN ContentType "
                            + "On ContentType.TypeCode = Contents.TypeCode Order by ContentType.ID DESC ";
            System.Data.DataTable dtContents = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);

            //if (dtContents.Rows.Count == 0)
            //{
            //    // 没有相应类型的信息
            //    RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            //    return;
            //}
            this.gv_ArticalList.DataSource = dtContents;
            this.gv_ArticalList.DataBind();
            viewState_DtSrc = dtContents;
        }
        else
        {
            this.ddl_ChildTypes.Visible = true;
            // generate datatable                       
            string sqlCmd = String.Format("SELECT * FROM ContentType WHERE ParentType = '{0}'", this.ddl_Types.SelectedValue);
            System.Data.DataTable dtChildTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            if (dtChildTypes == null || dtChildTypes.Rows.Count == 0)
            {
                this.ddl_ChildTypes.Visible = false;
                //
                // 比如，如果是网站公告的话，那就得绑定相应数据                //
                string sqlNobbCmd = String.Format("SELECT Contents.*, ContentType.TypeName FROM Contents INNER JOIN ContentType "
                           + "On ContentType.TypeCode = Contents.TypeCode WHERE Contents.TypeCode = '{0}' "
                           + "Order by ContentType.ID DESC", this.ddl_Types.SelectedValue);
                System.Data.DataTable dtContents = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlNobbCmd);
                //if (dtContents.Rows.Count == 0)
                //{
                //    // 没有相应类型的信息
                //    RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
                //    return;
                //}
                this.gv_ArticalList.DataSource = dtContents;
                this.gv_ArticalList.DataBind();
                viewState_DtSrc = dtContents;
                return;
            }
            this.ddl_ChildTypes.DataSource = dtChildTypes;
            this.ddl_ChildTypes.DataBind();
            this.ddl_ChildTypes.Items.Add(new ListItem("全部", "all"));
            this.ddl_ChildTypes.SelectedIndex = this.ddl_ChildTypes.Items.Count - 1;
            ddl_ChildTypes_SelectedIndexChanged(new Object(), new EventArgs());
        }
    }

    protected void gv_ArticalList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv_ArticalList.PageIndex = e.NewPageIndex;
        this.gv_ArticalList.DataSource = viewState_DtSrc;
        this.gv_ArticalList.DataBind();
    }

    protected void ddl_ChildTypes_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddl_ChildTypes.SelectedValue == "all")
        {
            string sqlCmd = String.Format("SELECT Contents.*, ContentType.TypeName FROM Contents INNER JOIN ContentType "
                            + "On ContentType.TypeCode = Contents.TypeCode WHERE ParentType = '{0}' "
                            + "Order by ContentType.ID DESC", this.ddl_Types.SelectedValue);
            System.Data.DataTable dtContents = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            //if (dtContents.Rows.Count == 0)
            //{
            //    // 没有相应类型的信息
            //    RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            //    return;
            //}
            this.gv_ArticalList.DataSource = dtContents;
            this.gv_ArticalList.DataBind();
            viewState_DtSrc = dtContents;
        }
        else
        {
            string sqlCmd = String.Format("SELECT Contents.*, ContentType.TypeName FROM Contents INNER JOIN ContentType "
                                + "On ContentType.TypeCode = Contents.TypeCode WHERE ContentType.TypeCode = '{0}' "
                                + "Order by ContentType.ID DESC ", this.ddl_ChildTypes.SelectedValue);
            System.Data.DataTable dtContents = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            //if (dtContents.Rows.Count == 0)
            //{
            //    // 没有相应类型的信息
            //    RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            //    return;
            //}
            this.gv_ArticalList.DataSource = dtContents;
            this.gv_ArticalList.DataBind();
            viewState_DtSrc = dtContents;
        }
    }

    protected string CutString(string title)
    {
        return Common.CutString(title);
    }
    //protected void gv_ArticalList_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    string ArticleID = gv_ArticalList.DataKeys[gv_ArticalList.SelectedIndex]["ID"].ToString();
    //    RegisterClientScriptBlock("tip", "<script>window.returnValue= "+ArticleID+";window.close();</script>");
    //}

    protected void gv_ArticalList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "SelectButton":
                //string ArticleID = gv_ArticalList.DataKeys[gv_ArticalList.SelectedIndex]["ID"].ToString();
                //ClientScript.RegisterClientScriptBlock(,"GoBack","<script>window.returnValue= " + e.CommandArgument.ToString() + ";window.close();</script>");
                //RegisterStartupScript("tip", "<script>window.returnValue= " + e.CommandArgument.ToString() + ";window.close();</script>");
                //RegisterStartupScript("tip", "<script>window.opener.document.all['ctl00_ContentPlaceHolder1_txb_LinkURL'].value='Content.aspx?articleID=" + e.CommandArgument.ToString() + "';window.close();</script>");
                ScriptManager.RegisterStartupScript(UpdatePanel1, Page.GetType(), 
                    "tip", "window.opener.document.all['ctl00_ContentPlaceHolder1_txb_LinkURL'].value='Content.aspx?articleID=" + e.CommandArgument.ToString() + "';window.close();", true);
                break;
            default:
                break;
        }
    }
}
